/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.admin.message.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.message.dto.MessageUserObjectSaveDTO;
import com.fuyao.cloud.admin.api.entity.SysDept;
import com.fuyao.cloud.admin.message.mapper.MessageUserobjectMapper;
import com.fuyao.cloud.admin.service.SysDeptService;
import com.fuyao.cloud.message.entity.MessageUserobject;
import com.fuyao.cloud.admin.message.service.MessageUserobjectService;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息对象
 *
 * @author liyue
 * @date 2022-04-22 17:28:09
 */
@Service
@AllArgsConstructor
public class MessageUserobjectServiceImpl extends ServiceImpl<MessageUserobjectMapper, MessageUserobject> implements MessageUserobjectService {

    private final SysDeptService sysDeptService;

    @Override
    public boolean save(MessageUserobject entity) {
        setEntityAttributes(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(MessageUserobject entity) {
        setEntityAttributes(entity);
        return super.updateById(entity);
    }

    @Override
    public boolean saveMessageUserObjectBatch(MessageUserObjectSaveDTO userObjectSaveDTO) {
        if (ObjectUtil.isEmpty(userObjectSaveDTO)) {
            return Boolean.FALSE;
        }
        List<MessageUserobject> entityList = Lists.newArrayList();
        userObjectSaveDTO.getUserList().forEach(saveDTO -> {
            MessageUserobject entity = new MessageUserobject();
            entity.setUserUserid(saveDTO.getUserId());
            entity.setUserName(saveDTO.getUsername());
            entity.setRealName(saveDTO.getRealName());
            entity.setUserOuid(saveDTO.getDeptId());
            entity.setEmail(saveDTO.getEmail());
            entity.setPhoneNumber(saveDTO.getRealPhone());
            entity.setSourceId(userObjectSaveDTO.getSourceId());
            entity.setSourceType(userObjectSaveDTO.getSourceType());
            entity.setManagementid(userObjectSaveDTO.getManagementid());
            setEntityAttributes(entity);
            entityList.add(entity);
        });
        return super.saveBatch(entityList);
    }

    private void setEntityAttributes(MessageUserobject entity) {
        SysDept dept = sysDeptService.getById(entity.getUserOuid());
        entity.setUserOuname(dept.getName());
        entity.setUserOufullname(dept.getFullName());
    }
}
